package com.distributed.netty.server;

import com.distributed.netty.encode.MessageDecoder;
import com.distributed.netty.encode.MessageEncoder;
import com.distributed.netty.entity.ElementJob;
import com.distributed.netty.entity.Message;
import com.rcache.core.cache.Rcache;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.serialization.ClassResolvers;
import io.netty.handler.codec.serialization.ObjectDecoder;
import io.netty.handler.codec.serialization.ObjectEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import org.apache.log4j.Logger;

import java.util.LinkedList;
import java.util.concurrent.TimeUnit;

public class HeartBeatServerInitializer extends ChannelInitializer<SocketChannel> {
	private Rcache rcache;
	private LinkedList<ElementJob> jobList;
	Logger LOG=Logger.getLogger(HeartBeatServerInitializer.class);
    public HeartBeatServerInitializer(Rcache rcache, LinkedList<ElementJob> jobList) {
		this.rcache=rcache;
		this.jobList=jobList;
    }

    @Override
	protected void initChannel(SocketChannel ch) throws Exception {
		LOG.debug("初始化化服务端通道……");
		ChannelPipeline pipe = ch.pipeline();
		pipe.addLast("idle",new IdleStateHandler(5,0,0,TimeUnit.SECONDS));
		pipe.addLast(new ObjectEncoder());
		pipe.addLast(new ObjectDecoder(ClassResolvers.weakCachingConcurrentResolver(Message.class.getClassLoader())));
//		pipe.addLast(new MessageEncoder());
//		pipe.addLast(new MessageDecoder());
		pipe.addLast("ChatHandler",new ChatHandler(rcache,jobList));
		pipe.addLast("channel",new HeartBeatServerHandler());
	}
}
